﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Markup;

namespace Geometry
{

    public class ElementExtension : MarkupExtension
    {
        public ElementExtension(string name)
        {
            this.Name = name;
        }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return new PlaceHolder(Name);
        }

        private string Name;
    }

    public class ElementReferenceSerializer : ValueSerializer
    {
        public override bool CanConvertToString(object value, IValueSerializerContext context)
        {
            return value is IGObject;
        }

        public override string ConvertToString(object value, IValueSerializerContext context)
        {
            //return "Element " + (value as IGObject).Name + "";
            return (value as IGObject).Name;
        }

        public override bool CanConvertFromString(string value, IValueSerializerContext context)
        {
            return base.CanConvertFromString(value, context);
        }

        public override object ConvertFromString(string value, IValueSerializerContext context)
        {
            return base.ConvertFromString(value, context);
        }
    }

    static class PlaceHolderExtensions
    {
        internal static bool IsNullOrPlaceHolder(this IGObject gobject)
        {
            return gobject == null || gobject is PlaceHolder;
        }
    }

    public class PlaceHolderTypeConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return base.CanConvertFrom(context, sourceType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            return new PlaceHolder(value as string);
        }
    }

    class PlaceHolder : IGObject, IGPoint, IGLine, IGCircle
    {
        public PlaceHolder(string name)
        {
            ElementName = name;
        }
        public readonly string ElementName;

        #region IGCircle Members

        double IGCircle.Radius
        {
            get { throw new NotImplementedException(); }
        }

        System.Windows.Point IGCircle.Centre
        {
            get { throw new NotImplementedException(); }
        }

        #endregion

        #region IGObject Members

        System.Collections.ObjectModel.ReadOnlyObservableCollection<IGObject> IGObject.Dependencies
        {
            get { throw new NotImplementedException(); }
        }

        System.Collections.ObjectModel.ReadOnlyObservableCollection<IGObject> IGObject.Dependents
        {
            get { throw new NotImplementedException(); }
        }

        string IGObject.Name
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        Rect? IGObject.BoundingBox
        {
            get
            {
                throw new NotImplementedException();
            }
        }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region IDisposable Members

        void IDisposable.Dispose()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IGLine Members

        double IGLine.Slope
        {
            get { throw new NotImplementedException(); }
        }

        System.Windows.Point IGLine.Point1
        {
            get { throw new NotImplementedException(); }
        }

        System.Windows.Point IGLine.Point2
        {
            get { throw new NotImplementedException(); }
        }

        #endregion

        #region IGPoint Members

        System.Windows.Point IGPoint.Position
        {
            get { throw new NotImplementedException(); }
        }

        double IGPoint.PosX
        {
            get { throw new NotImplementedException(); }
        }

        double IGPoint.PosY
        {
            get { throw new NotImplementedException(); }
        }

        #endregion
    }


}